package com.ag.wsq.service.impl;

import java.util.List;

import com.ag.bean.TbSched;
import com.ag.bean.TbSchedId;
import com.ag.common.AgException;
import com.ag.common.IBaseService;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.wsq.dao.SchedDAO;
import com.ag.wsq.dao.UtilDAO;

public class SchedServiceImpl implements IBaseService<TbSched, String> {
	
	private SchedDAO schedDAO;
	private UtilDAO utilDAO;

	public PageUtil queryAll(int total, int page, int maxRow, String UId, String UName, String timeStart, String timeEnd) {
		String cql = "select count(*) from TbSched p where p.a is null";
		String hql = "from TbSched p join fetch p.tbDuty join fetch" +
				" p.tbDetailShift join fetch p.tbUser where p.a is null";
		
		if (UId!=null&&UId.length() > 0){
			cql += " and p.id.UId = '" + UId + "'";
			hql += " and p.id.UId = '" + UId + "'";
		}
		if (UName!=null&& UName.length() > 0){
			cql += " and p.tbUser.UName = '" + UName + "'";
			hql += " and p.tbUser.UName = '" + UName + "'";
		}
		if (timeStart != null && timeStart.length() > 0) {
			cql += " and p.id.scTime >= :timeStart";
			hql += " and p.id.scTime >= :timeStart";
		}
		if (timeEnd != null && timeEnd.length() > 0) {
			cql += " and p.id.scTime <= :timeEnd";
			hql += " and p.id.scTime <= :timeEnd";
		}
		hql += " order by p.tbDuty.duId, p.tbDetailShift.dsName, p.id.scTime";
		PageUtil pageUtil = new PageUtil();
		List result = null;
		try {
			total = ((Long) this.utilDAO.queryCount(cql, timeStart, timeEnd).iterator().next()).intValue();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
System.out.println("------------------------------------");		
System.out.println("result size = " + total);
		
		if (page == 0) {
			page = 1;
		}
		if (maxRow == 0) {
			maxRow = 10;
		}
		int firstRow = (page - 1) * maxRow;
		try {
			result = this.utilDAO.query(hql, timeStart, timeEnd, firstRow, maxRow);
System.out.println("------------------------------------");	
System.out.println("result  = " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		pageUtil.setTotal(total);
		pageUtil.setPage(page);
		pageUtil.setMaxRow(maxRow);
		pageUtil.setList(result);
		return pageUtil;

	}
	
	public PageUtil query(String UId, String UName, String timeStart, String timeEnd){
		String hql = "from TbSched p join fetch p.tbDuty join fetch" +
				" p.tbDetailShift join fetch p.tbUser where p.a is null";
		
		if (UId!=null&&UId.length() > 0){
			hql += " and p.id.UId = " + UId;
		}
		if (UName!=null&& UName.length() > 0){
			hql += " and p.tbUser.UName = " + UName;
		}
		if (timeStart != null && timeStart.length() > 0) {
			hql += " and p.id.scTime >= :timeStart";
		}
		if (timeEnd != null && timeEnd.length() > 0) {
			hql += " and p.id.scTime <= :timeEnd";
		}
		hql += " order by p.tbDuty.duId, p.tbDetailShift.dsName, p.id.scTime";
		
		PageUtil pageUtil = new PageUtil();
		List result = null;
		try {
			result = this.utilDAO.queryCount(hql, timeStart, timeEnd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		pageUtil.setList(result);
		return pageUtil;
	}
	

	/**
	 * 根据ID删除值班人员
	 */
	public boolean delete(TbSchedId id) {
		try {
			TbSched sched = schedDAO.findById(id);
			sched.setA(IDUtil.DELETE);
			schedDAO.attachDirty(sched);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new AgException("删除角色失败！", e);
		}

	}

	public boolean delete(TbSched t) {
		return false;
	}

	public boolean save(TbSched t) {
		return false;
	}

	public boolean update(TbSched t) {
		return false;
	}

	public SchedDAO getSchedDAO() {
		return schedDAO;
	}

	public void setSchedDAO(SchedDAO schedDAO) {
		this.schedDAO = schedDAO;
	}


	@Override
	public TbSched findById(String id) {
		return null;
	}

	public UtilDAO getUtilDAO() {
		return utilDAO;
	}

	public void setUtilDAO(UtilDAO utilDAO) {
		this.utilDAO = utilDAO;
	}

	@Override
	public boolean delete(String id) {
		return false;
	}

}
